﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIAKADCORE.Controllers;

namespace SIAKAD
{
    public partial class ViewKuisioner : System.Web.UI.Page
    {
        private DataSet data;
        private Dictionary<string, string> jawabanKuisioner;
        private int count;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack || !Page.IsCallback)
            {
                this.LoadKuisioner();
            }
        }

        private void LoadKuisioner()
        {

            bool success = KuisionerController.GetTabelPreview("DPM0000001", out this.data);
            if (success)
            {
                this.jawabanKuisioner = new Dictionary<string, string>();
                this.count = this.data.Tables[0].Rows.Count;

                for (int i = 0; i < this.data.Tables[0].Rows.Count; i++)
                {
                    this.CreatePertanyaan(this.data.Tables[0].Rows[i] as DataRow, i);
                    this.CreateListJawaban(this.data.Tables[0].Rows[i] as DataRow, i);
                }
                this.CreateSubmitButton();
            }
        }

        private void CreatePertanyaan(DataRow dr, int index)
        {
            var isiPertanyaan = new Literal
            {
                ID = "pertanyaan" + index,
                Text = dr[3].ToString() + "<br/>\n"
            };
            this.pnlKuisioner.Controls.Add(isiPertanyaan);
        }

        private void CreateListJawaban(DataRow dr, int index)
        {
            for (int i = 0; i < 4; i++)
            {
                var radio = new RadioButton
                {
                    ID = "jawaban" + index + "_" + i,
                    CssClass = "radio",
                    GroupName = "jawaban" + index,
                    Text = dr[4 + i].ToString(),
                };

                radio.Attributes["value"] = "pilihan" + dr[8 + i].ToString();
                
                this.pnlKuisioner.Controls.Add(radio);
            }
        }

        private void CreateSubmitButton()
        {
            var submit = new Button
            {
                ID = "btSubmit",
                Text = "Submit"
            };

            submit.Click += (s, e) =>
            {
                this.ValidateKuisioner();
            };

            this.pnlKuisioner.Controls.Add(submit);
        }

        private void ValidateKuisioner()
        {
            foreach (var control in this.pnlKuisioner.Controls)
            {
                if (control is RadioButton)
                {
                    if ((control as RadioButton).Checked)
                    {
                        this.jawabanKuisioner.Add((control as RadioButton).GroupName, (control as RadioButton).Attributes["value"].Substring(7));
                    }
                }
            }

            if (this.jawabanKuisioner.Count < this.count)
            {
                System.Diagnostics.Debug.WriteLine("kurang");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("keisi semua");
            }
            
        }
    }
}